import {Column, Entity, JoinTable, ManyToMany, PrimaryGeneratedColumn} from "typeorm";
import {ApiProperty} from "@nestjs/swagger";
import {DefaultEnum} from "../common/enum/DefaultEnum";
import {StatusEnum, StatusEnumComment} from "../common/enum/StatusEnum";
import {BaseEntity} from "./BaseEntity";
import {SysRole} from "./SysRole";
import {IsNotEmpty} from "class-validator";
import {Validate_Mods} from "../common/enum/ValidateModsEnum";
import {Expose} from "class-transformer";


@Entity('sys_user')
export class SysUser extends BaseEntity {


    constructor(createBy?: number, createTime?: Date, updateBy?: number, updateTime?: Date,
                id?: number, username?: string, password?: string, name?: string, isDefault?: number, status?: number, roles?: SysRole[]) {
        super(createBy, createTime, updateBy, updateTime);
        this.id = id;
        this.username = username;
        this.password = password;
        this.name = name;
        this.isDefault = isDefault;
        this.status = status;
        this.roles = roles;
    }

    @ApiProperty({description: '主键ID', example: 1})
    @Expose({name: "userId"})
    @PrimaryGeneratedColumn({comment: '主键ID'})
    @IsNotEmpty({message: "id不能为空!", groups: [Validate_Mods.update, Validate_Mods.delete]})
    id: number;

    @ApiProperty({description: '账号', example: 'admin'})
    @Expose({name: "account"})
    @Column({comment: '账号', length: 50, nullable:false, unique: true })
    username: string;

    @ApiProperty({description: '密码', example: '123456'})
    @Expose({name: "pwd"})
    @Column({comment:'密码', length: 50, nullable: false})
    password: string;

    @ApiProperty({description: '姓名', example: '管理员'})
    @Expose({name: "userName"})
    @Column({comment: '姓名', length: 50, nullable: true})
    name: string;

    @ApiProperty({description: '是否默认（默认标识不可删除，仅管理员可操作）：0-否；1-是', example: 0})
    @Column({name:'is_default', comment: '是否默认（默认标识不可删除，仅管理员可操作）：0-否；1-是', default: 0})
    /** @see DefaultEnum */
    isDefault: number;

    @ApiProperty({description: StatusEnumComment, example: 1})
    @Column({comment: StatusEnumComment,  default: 1, })
    /** @see StatusEnum */
    status: number;

    /**
     * 关联关系
     */
    @ManyToMany(type => SysRole, role => role.users, {createForeignKeyConstraints: false})
    @JoinTable({ name: "sys_user_role", joinColumn: {name: "user_id", referencedColumnName: "id"}, inverseJoinColumn: {name: "role_id"} })
    @Expose({name: "roleList"})
    roles: SysRole[];
}
